﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Configuration; 

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RPTSiteOpeningRecords : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }
        #region Private Method

        private void BindGridView()
        { 
            RPTSiteOpeningRecordsService RPTSiteOpeningRecordsServiceObj = ServiceFactory.getRPTSiteOpeningRecordsService();
            OperationResult<List<tb_RPT_SiteOpeningRecord>> result = RPTSiteOpeningRecordsServiceObj.GetAllSiteOpeningRecords();

            if (result.operationSuccess)
            {
                gvSiteOpeningRecordsList.DataSource = result.obj;
                gvSiteOpeningRecordsList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            RPTSiteOpeningRecordsService RPTSiteOpeningRecordsServiceObj = ServiceFactory.getRPTSiteOpeningRecordsService();
            OperationResult<tb_RPT_SiteOpeningRecord> resultEdit = RPTSiteOpeningRecordsServiceObj.GetSiteOpeningRecordsById(id);

            if (resultEdit.operationSuccess == true)
            {
                txtEditWeather.Text = resultEdit.obj.Weather;
                txtEditInputDate.Text = resultEdit.obj.InputDate.ToString();
                txtEditProjectName.Text = resultEdit.obj.ProjectName.ToString();
                txtEditProjectUnit.Text = resultEdit.obj.ProjectUnit;
                txtEditConstructionSite.Text = resultEdit.obj.ConstructionSite;
                txtEditStrengthGrade.Text = resultEdit.obj.StrengthGrade;
                txtEditDesignSlump.Text = resultEdit.obj.DesignSlump;
                txtEditFactorySlump.Text = resultEdit.obj.FactorySlump;
                txtEditOpeningTime.Text = resultEdit.obj.OpeningTime.ToString();
                txtEditReserveVolume.Text = resultEdit.obj.ReserveVolume;
                txtEditUsingCement.Text = resultEdit.obj.UsingCement;
                txtEditProductionLine.Text = resultEdit.obj.ProductionLine;
                txtEditApproachTo.Text = resultEdit.obj.ApproachTo;
                txtEditCarryOn.Text = resultEdit.obj.CarryOn;

                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RPTSiteOpeningRecordsService RPTSiteOpeningRecordsServiceObj = ServiceFactory.getRPTSiteOpeningRecordsService();
            tb_RPT_SiteOpeningRecord instance = new tb_RPT_SiteOpeningRecord();

            instance.Weather =  txtWeather.Text;
            instance.InputDate = DateTime.Parse(  txtInputDate.Text);
            instance.ProjectName =  txtProjectName.Text;
            instance.ProjectUnit =  txtProjectUnit.Text;
            instance.ConstructionSite =  txtConstructionSite.Text;
            instance.StrengthGrade =  txtStrengthGrade.Text;
            instance.DesignSlump =  txtDesignSlump.Text;
            instance.FactorySlump = txtFactorySlump.Text;
            instance.OpeningTime = DateTime.Parse(  txtOpeningTime.Text);
            instance.ReserveVolume = txtReserveVolume.Text;
            instance.UsingCement = txtUsingCement.Text;
            instance.ProductionLine =  txtProductionLine.Text;
            instance.ApproachTo =  txtApproachTo.Text;
            instance.CarryOn = txtCarryOn.Text; 

            OperationResult<tb_RPT_SiteOpeningRecord> result = RPTSiteOpeningRecordsServiceObj.CreateSiteOpeningRecords(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            RPTSiteOpeningRecordsService RPTSiteOpeningRecordsServiceObj = ServiceFactory.getRPTSiteOpeningRecordsService();
            tb_RPT_SiteOpeningRecord entity = new tb_RPT_SiteOpeningRecord();

            entity.Id = editId;
            entity.Weather =txtEditWeather.Text;
            entity.InputDate =DateTime.Parse ( txtEditInputDate.Text);
            entity.ProjectName =txtEditProjectName.Text;
            entity.ProjectUnit =txtEditProjectUnit.Text;
            entity.ConstructionSite =txtEditConstructionSite.Text;
            entity.StrengthGrade =txtEditStrengthGrade.Text;
            entity.DesignSlump =txtEditDesignSlump.Text;
            entity.FactorySlump =txtEditFactorySlump.Text;
            entity.OpeningTime =DateTime.Parse (txtEditOpeningTime.Text);
            entity.ReserveVolume =txtEditReserveVolume.Text;
            entity.UsingCement =txtEditUsingCement.Text;
            entity.ProductionLine =txtEditProductionLine.Text;
            entity.ApproachTo =txtEditApproachTo.Text;
            entity.CarryOn = txtEditCarryOn.Text;  

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_RPT_SiteOpeningRecord> resultEdit = RPTSiteOpeningRecordsServiceObj.ModifySiteOpeningRecords(entity);

            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RPTSiteOpeningRecordsService RPTSiteOpeningRecordsServiceObj = ServiceFactory.getRPTSiteOpeningRecordsService();
            tb_RPT_SiteOpeningRecord deleteEntity = new tb_RPT_SiteOpeningRecord();
            deleteEntity.Id = deleteId;
            RPTSiteOpeningRecordsServiceObj.DeleteSiteOpeningRecords(deleteEntity);

            BindGridView();
        }

        protected void gvSiteOpeningRecordsList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvSiteOpeningRecordsList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSiteOpeningRecordsList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
